#include<iostream>  
using namespace std;
bool leapyear(int a) {
	if ((a % 4 == 0 && a % 100) || a % 400 == 0)
		return true;
	else
		return false;
}
int main() {
	int a[3] = { 0 };
	int n;
	cin >> n;
	int day[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	while (n--) {
		int dayNum = 0;
		for (int i = 0; i < 3; i++) {
			cin >> a[i];
		}
		if (leapyear(a[0]))
			day[1] = 29;
		else
			day[1] = 28;
		for (int i = 0; i + 1 < a[1]; i++) {
			dayNum += day[i];
		}
		cout << dayNum + a[2] << endl;
	}
}

